package com.rock.code.common.util;

import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhongqing.han Cotter on 2019/3/28.
 */
public class FormatValue {

    public static Map<String, String> formatLinuxValue(String key, String key_, JSONObject _json, String kpi_id) {

        HashMap<String, String> result = new HashMap<>();
        String kpi_value = null;
        String entity_id = null;
        String value = ZabbixUtil.getZabbixValueFromZabbixKey(_json);
        if (key.equals(key_)) {
//            String value = ZabbixUtil.getZabbixValueFromZabbixKey(_json);

            // 主机类型截取
            if ("system.uname".equals(key)) {
                kpi_value = value.split(" ")[0];
            }
            // 主机运行时长转换为天、时、分、秒
            if ("system.uptime".equals(key)) {
                int uptime = Integer.parseInt(value);
                kpi_value = uptime / (3600 * 24) + " day, "
                        + (uptime % (3600 * 24)) / 3600 + " hour, "
                        + (uptime % (3600 * 24) % 3600) / 60 + " minute, "
                        + uptime % (3600 * 24) % 3600 % 60 + " second ";
            }
            // 内存大小需要转换
            if ("vm.memory.size[total]".equals(key)) {
                Long memory = Long.parseLong(_json.getString("lastvalue"));
                kpi_value = String.format("%.2f", memory.doubleValue() / (1024 * 1024 * 1024));
            }
            // ping状态转换
            if ("icmpping[,20,20,64,]".equals(key)) {
                int ping = Integer.parseInt(_json.getString("lastvalue"));
                kpi_value = ping == 1 ? "ok" : "false";
            }
            // 交换分区大小
            if ("system.swap.size[,total]".equals(key)) {
                Long swap = Long.parseLong(_json.getString("lastvalue"));
                kpi_value = String.format("%.2f", swap.doubleValue() / (1024 * 1024));
            }
            // 交换分区剩余空间
            if ("system.swap.size[,free]".equals(key)) {
                Long swapFree = Long.parseLong(_json.getString("lastvalue"));
                kpi_value = String.format("%.2f", swapFree.doubleValue() / (1024 * 1024));
            }
            // 交换分区使用率=100-交换分区空闲率
            if ("system.swap.size[,pfree]".equals(key)) {
                kpi_value = 100.0f - Float.parseFloat(_json.getString("lastvalue")) + "";
            }

            if (kpi_value == null || "".equals(kpi_value)) {
                kpi_value = value;
            }

        } else if (key.startsWith(key_) && !"0".equals(value)) {
//            String value = ZabbixUtil.getZabbixValueFromZabbixKey(_json);
            // 文件系统
            if (key.startsWith("vfs.fs.inode[") && key.endsWith("pfree]")) {
                if ("CM-00-01-001-16".equals(kpi_id)) {
                    kpi_value = key.split(",")[0].substring(13);
                    entity_id = kpi_value;
                    System.out.println(kpi_value+"    "+entity_id);
                } else if ("PM-00-01-004-03".equals(kpi_id)) {
                    Double _value = 100.0f - Double.parseDouble(value);
                    kpi_value = String.format("%.2f", _value);
                    entity_id = key.split(",")[0].substring(13);
                    System.out.println(kpi_value+"    "+entity_id);
                }
            }

            // 磁盘
            if(key.startsWith("vfs.fs.size[") && key.endsWith("pfree]")){
                if ("CM-00-01-001-20".equals(kpi_id)) {
                    kpi_value = key.split(",")[0].substring(12);
                    entity_id = kpi_value;
                } else if ("PM-00-01-004-14".equals(kpi_id)) {
                    Double _value = 100.0f - Double.parseDouble(value);
                    kpi_value = String.format("%.2f", _value);
                    entity_id = key.split(",")[0].substring(12);
                }
            }
            if(key.startsWith("vfs.fs.size[") && key.endsWith("total]")){
                if ("PM-00-01-003-06".equals(kpi_id)) {
                    Long disk = Long.parseLong(value);
                    kpi_value = String.format("%.2f", disk.doubleValue()/(1024*1024*1024));
                    entity_id = key.split(",")[0].substring(12);
                }
            }
            if(key.startsWith("vfs.fs.size[") && key.endsWith("used]")){
                if ("PM-00-02-003-12".equals(kpi_id)) {
                    Long disk = Long.parseLong(value);
                    kpi_value = String.format("%.2f", disk.doubleValue()/(1024*1024*1024));
                    entity_id = key.split(",")[0].substring(12);
                }
            }
            if(key.startsWith("vfs.fs.size[") && key.endsWith(",free]")){
                if ("PM-00-02-003-13".equals(kpi_id)) {
                    Long disk = Long.parseLong(value);
                    kpi_value = String.format("%.2f", disk.doubleValue()/(1024*1024*1024));
                    entity_id = key.split(",")[0].substring(12);
                }
            }
        }

        result.put("kpi_value", kpi_value);
        result.put("entity_id", entity_id);

        return result;
    }

    public static Map<String, String> formatWindowsValue(String key, String key_, JSONObject _json, String kpi_id) {

        HashMap<String, String> result = new HashMap<>();
        String kpi_value = null;
        String entity_id = null;
        String value = ZabbixUtil.getZabbixValueFromZabbixKey(_json);
        if (key.equals(key_)) {
//            String value = ZabbixUtil.getZabbixValueFromZabbixKey(_json);

            // 主机类型截取
            if ("system.uname".equals(key)) {
                kpi_value = value.split(" ")[0];
            }
            // 主机运行时间需要转换
            if ("system.uptime".equals(key)) {
                int uptime = Integer.parseInt(value);
                kpi_value = uptime/(3600*24) + " day, "
                        + (uptime%(3600*24))/3600 + " hour, "
                        + (uptime%(3600*24)%3600)/60 + " minute, "
                        + uptime%(3600*24)%3600%60 + " second ";
            }
            // 主机内存大小需要转换
            if ("vm.memory.size[total]".equals(key)) {
                Long memory = Long.parseLong(value);
                kpi_value = String.format("%.2f", memory.doubleValue()/(1024*1024*1024));
            }
            // ping状态转换
            if ("icmpping[,20,20,64,]".equals(key)) {
                int ping = Integer.parseInt(value);
                kpi_value = ping == 1 ? "ok" : "false";
            }
            // 交换分区大小
            if ("jmx[\"java.lang:type=OperatingSystem\",TotalSwapSpaceSize]".equals(key)) {
                Long swap = Long.parseLong(value);
                kpi_value = String.format("%.2f", swap.doubleValue()/(1024*1024));
            }
            // 交换分区剩余空间
            if ("jmx[\"java.lang:type=OperatingSystem\",FreeSwapSpaceSize]".equals(key)) {
                Long swapFree = Long.parseLong(value);
                kpi_value = String.format("%.2f", swapFree.doubleValue()/(1024*1024));
            }

            if (kpi_value == null || "".equals(kpi_value)) {
                kpi_value = value;
            }

        } else if (key.startsWith(key_) && !"0".equals(value)) {
//            String value = ZabbixUtil.getZabbixValueFromZabbixKey(_json);

            // 磁盘
            if(key.startsWith("vfs.fs.size[") && key.endsWith("pfree]")){
                if ("CM-00-01-001-20".equals(kpi_id)) {
                    kpi_value = key.split(",")[0].substring(12);
                    entity_id = kpi_value;
                } else if ("PM-00-01-004-14".equals(kpi_id)) {
                    Double _value = 100.0f - Double.parseDouble(value);
                    kpi_value = String.format("%.2f", _value);
                    entity_id = key.split(",")[0].substring(12);
                }
            }

            // 磁盘
            if(key.startsWith("vfs.fs.size[") && key.endsWith("pfree]")){
                if ("CM-00-01-001-20".equals(kpi_id)) {
                    kpi_value = key.split(",")[0].substring(12);
                    entity_id = kpi_value;
                } else if ("PM-00-01-004-14".equals(kpi_id)) {
                    Double _value = 100.0f - Double.parseDouble(value);
                    kpi_value = String.format("%.2f", _value);
                    entity_id = key.split(",")[0].substring(12);
                }
            }
            if(key.startsWith("vfs.fs.size[") && key.endsWith("total]")){
                if ("PM-00-01-003-06".equals(kpi_id)) {
                    Long disk = Long.parseLong(value);
                    kpi_value = String.format("%.2f", disk.doubleValue()/(1024*1024*1024));
                    entity_id = key.split(",")[0].substring(12);
                }
            }
            if(key.startsWith("vfs.fs.size[") && key.endsWith("used]")){
                if ("PM-00-02-003-12".equals(kpi_id)) {
                    Long disk = Long.parseLong(value);
                    kpi_value = String.format("%.2f", disk.doubleValue()/(1024*1024*1024));
                    entity_id = key.split(",")[0].substring(12);
                }
            }
            if(key.startsWith("vfs.fs.size[") && key.endsWith(",free]")){
                if ("PM-00-02-003-13".equals(kpi_id)) {
                    Long disk = Long.parseLong(value);
                    kpi_value = String.format("%.2f", disk.doubleValue()/(1024*1024*1024));
                    entity_id = key.split(",")[0].substring(12);
                }
            }
        }

        result.put("kpi_value", kpi_value);
        result.put("entity_id", entity_id);

        return result;
    }

    public static Map<String, String> formatHpValue(String key, String key_, JSONObject _json, String kpi_id) {

        HashMap<String, String> result = new HashMap<>();
        String kpi_value = null;
        String entity_id = null;
        String value = ZabbixUtil.getZabbixValueFromZabbixKey(_json);
        if (key.equals(key_)) {
//            String value = ZabbixUtil.getZabbixValueFromZabbixKey(_json);

            // 主机类型截取
            if ("system.uname".equals(key)) {
                kpi_value = value.split(" ")[0];
            }
            // 主机运行时间需要转换
            if ("system.uptime".equals(String.valueOf(_json.get("key_")))) {
                int uptime = Integer.parseInt(value);
                kpi_value = uptime/(3600*24) + " day, "
                        + (uptime%(3600*24))/3600 + " hour, "
                        + (uptime%(3600*24)%3600)/60 + " minute, "
                        + uptime%(3600*24)%3600%60 + " second ";
            }
            // 主机内存大小需要转换
            if ("vm.memory.size[total]".equals(key)) {
                Long memory = Long.parseLong(value);
                kpi_value = String.format("%.2f", memory.doubleValue()/(1024*1024*1024));
            }
            // ping状态转换
            if ("icmpping[,20,20,64,]".equals(key)) {
                int ping = Integer.parseInt(value);
                kpi_value = ping == 1 ? "ok" : "false";
            }
            // 交换分区大小
            if ("system.swap.size[,total]".equals(key)) {
                Long swap = Long.parseLong(value);
                kpi_value = String.format("%.2f", swap.doubleValue()/(1024*1024));
            }
            // 交换分区剩余空间
            if ("system.swap.size[,free]".equals(key)) {
                Long swapFree = Long.parseLong(value);
                kpi_value = String.format("%.2f", swapFree.doubleValue()/(1024*1024));
            }
            // 交换分区使用率=100-交换分区空闲率
            if ("system.swap.size[,pfree]".equals(key)) {
                kpi_value = 100.0f - Float.parseFloat(value) + "";
            }

            if (kpi_value == null || "".equals(kpi_value)) {
                kpi_value = value;
            }

        } else if (key.startsWith(key_) && !"0".equals(value)) {
//            String value = ZabbixUtil.getZabbixValueFromZabbixKey(_json);

            //文件系统
            if (key.startsWith("vfs.fs.inode[") && key.endsWith("pfree]")) {
                if ("CM-00-01-001-16".equals(kpi_id)) {
                    kpi_value = key.split(",")[0].substring(13);
                    entity_id = kpi_value;
                } else if ("PM-00-01-004-03".equals(kpi_id)) {
                    Double _value = 100.0f - Double.parseDouble(value);
                    kpi_value = String.format("%.2f", _value);
                    entity_id = key.split(",")[0].substring(13);
                }
            }

            // 磁盘
            if(key.startsWith("vfs.fs.size[") && key.endsWith("pfree]")){
                if ("CM-00-01-001-20".equals(kpi_id)) {
                    kpi_value = key.split(",")[0].substring(12);
                    entity_id = kpi_value;
                } else if ("PM-00-01-004-14".equals(kpi_id)) {
                    Double _value = 100.0f - Double.parseDouble(value);
                    kpi_value = String.format("%.2f", _value);
                    entity_id = key.split(",")[0].substring(12);
                }
            }
            if(key.startsWith("vfs.fs.size[") && key.endsWith("total]")){
                if ("PM-00-01-003-06".equals(kpi_id)) {
                    Long disk = Long.parseLong(value);
                    kpi_value = String.format("%.2f", disk.doubleValue()/(1024*1024*1024));
                    entity_id = key.split(",")[0].substring(12);
                }
            }
            if(key.startsWith("vfs.fs.size[") && key.endsWith("used]")){
                if ("PM-00-02-003-12".equals(kpi_id)) {
                    Long disk = Long.parseLong(value);
                    kpi_value = String.format("%.2f", disk.doubleValue()/(1024*1024*1024));
                    entity_id = key.split(",")[0].substring(12);
                }
            }
            if(key.startsWith("vfs.fs.size[") && key.endsWith(",free]")){
                if ("PM-00-02-003-13".equals(kpi_id)) {
                    Long disk = Long.parseLong(value);
                    kpi_value = String.format("%.2f", disk.doubleValue()/(1024*1024*1024));
                    entity_id = key.split(",")[0].substring(12);
                }
            }
        }

        result.put("kpi_value", kpi_value);
        result.put("entity_id", entity_id);

        return result;
    }

    public static Map<String, String> formatAIXValue(String key, String key_, JSONObject _json, String kpi_id) {

        HashMap<String, String> result = new HashMap<>();
        String kpi_value = null;
        String entity_id = null;
        String value = ZabbixUtil.getZabbixValueFromZabbixKey(_json);
        if (key.equals(key_)) {
//            String value = ZabbixUtil.getZabbixValueFromZabbixKey(_json);

            // 主机类型截取
            if ("system.uname".equals(key)) {
                kpi_value = value.split(" ")[0];
            }
            // 主机运行时间需要转换
            if ("system.uptime".equals(key)) {
                int uptime = Integer.parseInt(value);
                kpi_value = uptime/(3600*24) + " day, "
                        + (uptime%(3600*24))/3600 + " hour, "
                        + (uptime%(3600*24)%3600)/60 + " minute, "
                        + uptime%(3600*24)%3600%60 + " second ";
            }
            // 主机内存大小需要转换
            if ("vm.memory.size[total]".equals(key)) {
                Long memory = Long.parseLong(value);
                kpi_value = String.format("%.2f", memory.doubleValue()/(1024*1024*1024));
            }
            // ping状态转换
            if ("icmpping[,20,20,64,]".equals(key)) {
                int ping = Integer.parseInt(value);
                kpi_value = ping == 1 ? "ok" : "false";
            }
            // 交换分区大小
            if ("system.swap.size[,total]".equals(key)) {
                Long swap = Long.parseLong(value);
                kpi_value = String.format("%.2f", swap.doubleValue()/(1024*1024));
            }
            // 交换分区剩余空间
            if ("system.swap.size[,free]".equals(key)) {
                Long swapFree = Long.parseLong(value);
                kpi_value = String.format("%.2f", swapFree.doubleValue()/(1024*1024));
            }
            // 交换分区使用率=100-交换分区空闲率
            if ("system.swap.size[,pfree]".equals(key)) {
                kpi_value = 100.0f - Float.parseFloat(value) + "";
            }

            if (kpi_value == null || "".equals(kpi_value)) {
                kpi_value = value;
            }

        } else if (key.startsWith(key_) && !"0".equals(value)) {
//            String value = ZabbixUtil.getZabbixValueFromZabbixKey(_json);

            // 文件系统
            if (key.startsWith("vfs.fs.inode[") && key.endsWith("pfree]")) {
                if ("CM-00-01-001-16".equals(kpi_id)) {
                    kpi_value = key.split(",")[0].substring(13);
                    entity_id = kpi_value;
                } else if ("PM-00-01-004-03".equals(kpi_id)) {
                    Double _value = 100.0f - Double.parseDouble(value);
                    kpi_value = String.format("%.2f", _value);
                    entity_id = key.split(",")[0].substring(13);
                }
            }

            // 磁盘
            if(key.startsWith("vfs.fs.size[") && key.endsWith("pfree]")){
                if ("CM-00-01-001-20".equals(kpi_id)) {
                    kpi_value = key.split(",")[0].substring(12);
                    entity_id = kpi_value;
                } else if ("PM-00-01-004-14".equals(kpi_id)) {
                    Double _value = 100.0f - Double.parseDouble(value);
                    kpi_value = String.format("%.2f", _value);
                    entity_id = key.split(",")[0].substring(12);
                }
            }
            if(key.startsWith("vfs.fs.size[") && key.endsWith("total]")){
                if ("PM-00-01-003-06".equals(kpi_id)) {
                    Long disk = Long.parseLong(value);
                    kpi_value = String.format("%.2f", disk.doubleValue()/(1024*1024*1024));
                    entity_id = key.split(",")[0].substring(12);
                }
            }
            if(key.startsWith("vfs.fs.size[") && key.endsWith("used]")){
                if ("PM-00-02-003-12".equals(kpi_id)) {
                    Long disk = Long.parseLong(value);
                    kpi_value = String.format("%.2f", disk.doubleValue()/(1024*1024*1024));
                    entity_id = key.split(",")[0].substring(12);
                }
            }
            if(key.startsWith("vfs.fs.size[") && key.endsWith(",free]")){
                if ("PM-00-02-003-13".equals(kpi_id)) {
                    Long disk = Long.parseLong(value);
                    kpi_value = String.format("%.2f", disk.doubleValue()/(1024*1024*1024));
                    entity_id = key.split(",")[0].substring(12);
                }
            }

        }

        result.put("kpi_value", kpi_value);
        result.put("entity_id", entity_id);

        return result;
    }

}
